package com.elong.businesstravel.modules.home;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.R.string;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ExpandableListView.OnChildClickListener;

import com.android.support.jhf.dialog.CustomWaitDialog.OnKeyCancel;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.iphonetreeview.IphoneTreeView;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.android.support.jhf.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.android.support.jhf.utils.CoordinateConversion;
import com.android.support.jhf.utils.FileUtils;
import com.android.support.jhf.utils.ToolUtils;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.Constant;
import com.elong.businesstravel.base.baseactivity.BaseActivity;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.entitys.AllCityPoi;
import com.elong.businesstravel.entitys.City;
import com.elong.businesstravel.entitys.CityPoi;
import com.elong.businesstravel.entitys.Poi;
import com.elong.businesstravel.modules.home.adapter.SimpleListAdapter;
import com.elong.businesstravel.protocol.city.GetPoiByCityAnalyzeJson;
import com.elong.businesstravel.protocol.city.GetPoiByCityAnalyzeJson.IPoiByCityAnalyze;

/**
 * 根据城市选择酒店位置
 * 
 * @author lenovo
 * 
 */
public class HotelLocationSelectActivity extends BaseActivity {

	private ListView mListView;
	private SimpleListAdapter mSimpleListAdapter;

	private LinearLayout mTopLayout;
	/** 热门商圈 */
	private LinearLayout mLayout1;
	/** 机场/火车站 */
	private LinearLayout mLayout2;
	/** 地铁 */
	private LinearLayout mLayout3;
	/** 行政区 */
	private LinearLayout mLayout4;

	private City mSearchConditionCity;

	/** 0:热门商圈，1机场火车站，2地铁，3行政区 */
	private int mFlag = 0;

	private String lat;
	private String lng;
	private AllCityPoi mAllCityPoi;
	private List<Poi> mShangquanPoiList = new ArrayList<Poi>();
	private List<Poi> mXingzhengquPoiList = new ArrayList<Poi>();
	private List<Poi> mJichangHuochezhanPoiList = new ArrayList<Poi>();
	private List<CityPoi> mSubwayList = new ArrayList<CityPoi>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hotel_location_select);

		init();
	}

	@Override
	protected void init() {

		if (null != mApplication.mBdLocation) {
			lat = mApplication.mBdLocation.getLatitude() + "";
			lng = mApplication.mBdLocation.getLongitude() + "";
		}

		if (null == mSavedInstanceState) {
			mSearchConditionCity = (City) getIntent().getSerializableExtra(
					"city");
		} else {
			mSearchConditionCity = (City) mSavedInstanceState
					.getSerializable("searchcity");
			lat = mSavedInstanceState.getString("lat");
			lng = mSavedInstanceState.getString("lng");
		}

		mTopLayout = (LinearLayout) findViewById(R.id.topLayout);
		mLayout1 = (LinearLayout) findViewById(R.id.layout1);
		mLayout2 = (LinearLayout) findViewById(R.id.layout2);
		mLayout3 = (LinearLayout) findViewById(R.id.layout3);
		mLayout4 = (LinearLayout) findViewById(R.id.layout4);

		mListView = (ListView) findViewById(R.id.listView);

		setTitleNavBar();

		setListener();

		getPoiByCityId(mSearchConditionCity.cityId);
	}

	private void setSelectButton(int viewId) {
		Button button1 = (Button) findViewById(R.id.button1);
		Button button2 = (Button) findViewById(R.id.button2);
		Button button3 = (Button) findViewById(R.id.button3);
		Button button4 = (Button) findViewById(R.id.button4);

		if (R.id.button1 == viewId) {
			button1.setSelected(true);
			button2.setSelected(false);
			button3.setSelected(false);
			button4.setSelected(false);
		} else if (R.id.button2 == viewId) {
			button1.setSelected(false);
			button2.setSelected(true);
			button3.setSelected(false);
			button4.setSelected(false);
		} else if (R.id.button3 == viewId) {
			button1.setSelected(false);
			button2.setSelected(false);
			button3.setSelected(true);
			button4.setSelected(false);
		} else if (R.id.button4 == viewId) {
			button1.setSelected(false);
			button2.setSelected(false);
			button3.setSelected(false);
			button4.setSelected(true);
		}
	}

	@Override
	protected void setListener() {

		Button button1 = (Button) findViewById(R.id.button1);
		button1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 热门商圈
				setShangquanListData();
				setSelectButton(v.getId());
			}
		});
		Button button2 = (Button) findViewById(R.id.button2);
		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 机场/火车站
				setJichangHuochezhanListData();
				setSelectButton(v.getId());
			}
		});
		Button button3 = (Button) findViewById(R.id.button3);
		button3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 地铁
				setSubwayListData();
				setSelectButton(v.getId());
			}
		});
		Button button4 = (Button) findViewById(R.id.button4);
		button4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 行政区
				setXingzhengquListData();
				setSelectButton(v.getId());
			}
		});

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Poi poi = null;
				Intent intent = new Intent();
				if (0 == mFlag) {
					// 热门商圈
					poi = mShangquanPoiList.get(arg2);
					intent.putExtra("Poi", poi);
					setResult(RESULT_OK, intent);
					quit();
				} else if (1 == mFlag) {
					// 机场火车站
					poi = mJichangHuochezhanPoiList.get(arg2);
					intent.putExtra("Poi", poi);
					setResult(RESULT_OK, intent);
					quit();
				} else if (2 == mFlag) {
					// 地铁
					intent.setClass(mContext, SelectSubwayActivity.class);
					intent.putExtra("Pois",
							(ArrayList<Poi>) mSubwayList.get(arg2).pois);
					startActivityForResult(intent, 0);
					overridePendingTransition(
							R.anim.activity_bottom_top_enter_anim_translate,
							R.anim.activity_alpha_fixed);
				} else if (3 == mFlag) {
					// 行政区
					poi = mXingzhengquPoiList.get(arg2);
					intent.putExtra("Poi", poi);
					setResult(RESULT_OK, intent);
					quit();
				}
			}
		});

	}

	/**
	 * 显示商圈列表
	 */
	private void setShangquanListData() {
		mFlag = 0;

		List<String> strings = new ArrayList<String>();
		for (Poi poi : mShangquanPoiList) {
			String nameString = poi.name;
			if ("".equals(nameString)) {
				nameString = poi.bussinename;
			}
			if ("".equals(nameString)) {
				nameString = poi.districtsname;
			}
			strings.add(nameString);
		}

		mSimpleListAdapter = new SimpleListAdapter(mContext, strings, "");
		mListView.setAdapter(mSimpleListAdapter);
	}

	/**
	 * 显示机场火车站列表
	 */
	private void setJichangHuochezhanListData() {
		mFlag = 1;

		List<String> strings = new ArrayList<String>();
		for (Poi poi : mJichangHuochezhanPoiList) {
			String nameString = poi.name;
			if ("".equals(nameString)) {
				nameString = poi.bussinename;
			}
			if ("".equals(nameString)) {
				nameString = poi.districtsname;
			}
			strings.add(nameString);
		}
		mSimpleListAdapter = new SimpleListAdapter(mContext, strings, "");
		mListView.setAdapter(mSimpleListAdapter);
	}

	/**
	 * 显示地铁列表
	 */
	private void setSubwayListData() {
		mFlag = 2;

		List<String> strings = new ArrayList<String>();
		for (CityPoi cityPoi : mSubwayList) {
			strings.add(cityPoi.name);
		}
		mSimpleListAdapter = new SimpleListAdapter(mContext, strings, "", true);
		mListView.setAdapter(mSimpleListAdapter);
	}

	/**
	 * 显示行政区列表
	 */
	private void setXingzhengquListData() {
		mFlag = 3;

		List<String> strings = new ArrayList<String>();
		for (Poi poi : mXingzhengquPoiList) {
			String nameString = poi.name;
			if ("".equals(nameString)) {
				nameString = poi.bussinename;
			}
			if ("".equals(nameString)) {
				nameString = poi.districtsname;
			}
			strings.add(nameString);
		}
		mSimpleListAdapter = new SimpleListAdapter(mContext, strings, "");
		mListView.setAdapter(mSimpleListAdapter);
	}

	/**
	 * 根据城市id获取poi
	 */
	private void getPoiByCityId(String cityId) {
		showWaitDialog("正在载入请等待...", new OnKeyCancel() {

			@Override
			public void onKeyCancelListener() {
				quit();
			}
		});
		// new GetPoiByCityAnalyzeJson(cityId, mContext, new IPoiByCityAnalyze()
		// {
		new GetPoiByCityAnalyzeJson().GetPoiByCity(cityId, mContext,
				new IPoiByCityAnalyze() {

					@Override
					public void onSucceed(AllCityPoi allCityPoi) {
						// 解析成功，刷新列表
						mAllCityPoi = allCityPoi;

						for (int i = 0; i < mAllCityPoi.cityPoiList.size(); i++) {
							if ("商业区".equals(mAllCityPoi.cityPoiList.get(i).name)) {

								mShangquanPoiList
										.addAll(mAllCityPoi.cityPoiList.get(i).pois);
								Poi buxianpoi = new Poi();
								buxianpoi.name = Constant.NO_LIMIT;
								mShangquanPoiList.add(0, buxianpoi);
								if (null != mApplication.mLocationCity 
										&& null != mApplication.mBdLocation
										&& mApplication.mLocationCity.cityId.equals(mSearchConditionCity.cityId)
										&& null != lat && null != lng) {
//									double[] lngLat = CoordinateConversion
//											.bd2gg(mApplication.mBdLocation
//													.getLatitude(),
//													mApplication.mBdLocation
//															.getLongitude());
									double[] lngLat = CoordinateConversion
											.bd2gg(Double.parseDouble(lat),
													Double.parseDouble(lng));
									Poi poi = new Poi();
									poi.name = "当前位置";
									poi.googlelat = lngLat[1];
									poi.googlelng = lngLat[0];
									mShangquanPoiList.add(0, poi);
								}
								HandlerPostUI.getHandlerPostUI(new Runnable() {

									@Override
									public void run() {
										setShangquanListData();
										setSelectButton(R.id.button1);
									}
								});

							} else if ("行政区".equals(mAllCityPoi.cityPoiList
									.get(i).name)) {

								mXingzhengquPoiList
										.addAll(mAllCityPoi.cityPoiList.get(i).pois);

							} else if ("机场/火车站".equals(mAllCityPoi.cityPoiList
									.get(i).name)) {

								mJichangHuochezhanPoiList
										.addAll(mAllCityPoi.cityPoiList.get(i).pois);

							} else if ("地铁".equals(mAllCityPoi.cityPoiList
									.get(i).name)) {

								mSubwayList.addAll(mAllCityPoi.cityPoiList
										.get(i).subwayCityPois);

							}
						}
						hideWaitDialog();
					}
				});
	}

	@Override
	protected void setTitleNavBar() {

		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("选择位置");
		titleNavBarView.setTitleBackground(R.color.bg_title_dialog);
		titleNavBarView.setLeftButtonImageString(0, "取消", R.color.text_blue);
		titleNavBarView
				.setLeftButtonOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						quit();
					}
				});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 0: {
			// 选择地铁返回
			switch (resultCode) {
			case RESULT_OK: {
				Poi poi = (Poi) data.getSerializableExtra("Poi");
				Intent intent = new Intent();
				intent.putExtra("Poi", poi);
				setResult(RESULT_OK, intent);
				quit();
				break;
			}
			case RESULT_CANCELED: {
				break;
			}
			default:
				break;
			}
			break;
		}
		default:
			break;
		}
	}

	private void quit() {
		finish();
		overridePendingTransition(R.anim.activity_alpha_fixed,
				R.anim.activity_top_bottom_exit_anim_translate);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			quit();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putSerializable("searchcity", mSearchConditionCity);
		if (null != lat && null != lng) {
			outState.putString("lat", lat);
			outState.putString("lng", lng);
		}
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		if (null != savedInstanceState) {
			mSearchConditionCity = (City) savedInstanceState
					.getSerializable("searchcity");
			lat = savedInstanceState.getString("lat");
			lng = savedInstanceState.getString("lng");
		}
	}

}
